<?php
/**
 * Created by PhpStorm.
 * User: bolle
 * Date: 28.12.15
 * Time: 09:54
 */

namespace tbollmeier\alteraparser\syntaxgraph;


class Rule extends VertexGroup
{
    private $ruleFn;

    public function __construct($name='', $ruleFn=null, $optParams=[])
    {
        parent::__construct();

        $this->start->name = $name;
        $this->end->name = $name;
        $this->ruleFn = $ruleFn;

        $transformAstFn = $this->getParam($optParams, 'transformAstFn', null);
        if ($transformAstFn) {
            $this->end->transformAstFn = $transformAstFn;
        }

        $this->end->isEndOfUniqueRule = $this->getParam($optParams, 'isUnique', false);

    }

    protected function onCopy($original)
    {
        parent::onCopy($original);
        $this->ruleFn = $original->ruleFn;
    }

    protected function onExpand(&$start, &$end)
    {
        if ($this->ruleFn) {
            $this->ruleFn($start, $end);
        }
    }

    private function getParam($params, $name, $default)
    {
        return array_key_exists($name, $params) ? $params[$name] : $default;
    }

}